package com.lyb.spzx.manager.service.impl;

import cn.hutool.core.date.DateUtil;
import com.lyb.spzx.manager.mapper.OrderStatisticsMapper;
import com.lyb.spzx.manager.service.OrderInfoService;
import com.lyb.spzx.model.dto.order.OrderStatisticsDto;
import com.lyb.spzx.model.entity.order.OrderStatistics;
import com.lyb.spzx.model.vo.order.OrderStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    OrderStatisticsMapper orderStatisticsMapper;
    @Override
    public OrderStatisticsVo getOrderStatisticeData(OrderStatisticsDto orderStatisticsDto) {
        //查询统计结果数据
        List<OrderStatistics> orderStatistics = orderStatisticsMapper.selectList(orderStatisticsDto);
        //日期列表
        List<String> dateList = orderStatistics.stream()
                .map(orderStatistics1 -> DateUtil.format(orderStatistics1.getOrderDate(), "yyyy-MM-dd"))
                .collect(Collectors.toList());
        //统计金额列表
        List<BigDecimal> amountList = orderStatistics.stream().map(OrderStatistics::getTotalAmount).collect(Collectors.toList());
        //创建OrderStatisticsVo对象封装响应结果数据
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(amountList);
        //返回结果
        return orderStatisticsVo;
    }
}
